﻿using CoreShop.Library;
using CoreShop.Model;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;

namespace CoreShop.App
{
    public partial class UIFoodPackItem : UIUserControl
    {
        public corecmsfoodpack foodpack;
        public UIFoodPackItem(corecmsfoodpack _foodpack)
        {
            foodpack = _foodpack;
            InitializeComponent();
            this.lblSelectMsg.Left = this.pnlSelectType.Left;
            if (this.foodpack.packdetails != null )
            {
                foreach (var item in this.foodpack.packdetails)
                {
                    item.detailId = item.id;
                    if (item.detailId == 0) item.detailId = (long)Yitter.IdGenerator.YitIdHelper.NextId();
                    item.operate = "删除";
                }
            }            

            #region 加载状态信息
            this.selectType.Items.Clear();
            var mSelectType = EnumHelper.EnumToList<GlobalEnumVars.FoodPackSelectType>();
            selectType.DataSource = mSelectType;
            selectType.DisplayMember = "description";
            selectType.ValueMember = "value";
            this.selectType.SelectedValue = foodpack.selectType.ObjToInt(0);
            #endregion

            if (foodpack.id > 0)
            {
                this.lblPackName.Text = this.foodpack.name;
                this.selectType.SelectedValue = this.foodpack.selectType;
                this.quantityStar.Text = this.foodpack.quantityStar.ToString();
                this.quantityEnd.Text = this.foodpack.quantityEnd.ToString();
                this.isSelectSame.Checked = this.foodpack.isSelectSame;
            }

            this.binDetail.DataSource = _foodpack.packdetails;
            this.dgvFoodList.DataSource = this.binDetail;
        }

        public corecmsfoodpack GetFoodPackValue()
        {
            this.foodpack.name = this.lblPackName.Text;
            this.foodpack.selectType = this.selectType.SelectedValue.ObjToInt(0);
            this.foodpack.quantityStar = this.quantityStar.Text.ObjToInt(0);
            this.foodpack.quantityEnd = this.quantityEnd.Text.ObjToInt(0);
            this.foodpack.isSelectSame = this.isSelectSame.Checked;
            //List<corecmsfoodpackdetail> details = this.binDetail.DataSource as List<corecmsfoodpackdetail>;
            //this.foodpack.packdetails = details;
            return this.foodpack;
        }

        public event EventHandler DeletePack;
        private void btnDelete_Click(object sender, System.EventArgs e)
        {
            DeletePack?.Invoke(this, e);
        }

        private void selectType_SelectedValueChanged(object sender, EventArgs e)
        {
            var mSelectId = this.selectType.SelectedValue.ObjToInt(0);
            switch (mSelectId)
            {
                case (int)GlobalEnumVars.FoodPackSelectType.Optional:
                    pnlSelectType.Visible = true;
                    lblSelectMsg.Visible = false;
                    break;
                case (int)GlobalEnumVars.FoodPackSelectType.Fixed:
                    pnlSelectType.Visible = false;
                    lblSelectMsg.Visible = true;
                    lblSelectMsg.Text = "固定选择该组所有商品";
                    break;
                case (int)GlobalEnumVars.FoodPackSelectType.Wilfully:
                    pnlSelectType.Visible = false;
                    lblSelectMsg.Visible = true;
                    lblSelectMsg.Text = "任选该组商品明细中的一项";
                    break;
            }            
        }

        private void dgvFoodList_CellContentClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            var packdetails = this.binDetail.DataSource as List<corecmsfoodpackdetail>;
            int mRowIndex = e.RowIndex;
            if (dgvFoodList.Columns[e.ColumnIndex].Name == "operate")
            {
                Int64 detailId = CommFunc.ConvertToInt64(dgvFoodList.Rows[mRowIndex].Cells["detailId"].Value);
                var model = packdetails.Find(p => p.detailId == detailId);
                if (model != null)
                {
                    this.binDetail.DataSource = null;
                    packdetails.Remove(model);
                    this.binDetail.DataSource = packdetails;
                }
            }
        }

        private void btnAddFood_Click(object sender, EventArgs e)
        {
            var frm = new frmSelectFoods((int)Library.GlobalEnumVars.BillType.PackDetail, "套餐明细选择");
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                addPackDetail(frm.orderingDetail);
            }
            frm.Dispose();
        }

        private void addPackDetail(List<corecmsfoodordering> orderingDetails)
        {
            List<corecmsfoodpackdetail> details = this.binDetail.DataSource as List<corecmsfoodpackdetail>;
            this.binDetail.DataSource = null;
            if (foodpack.packdetails == null) foodpack.packdetails = new List<corecmsfoodpackdetail>();
            foreach (var food in orderingDetails)
            {
                var model = foodpack.packdetails.Find(p => p.foodId == food.foodId);
                if (model == null)
                {
                    var foodpackdetail = new corecmsfoodpackdetail();
                    foodpackdetail.id = 0;
                    foodpackdetail.detailId = (long)Yitter.IdGenerator.YitIdHelper.NextId();
                    foodpackdetail.foodId = food.foodId;
                    foodpackdetail.foodSpecId = food.foodSpecId;
                    foodpackdetail.foodName = food.name;
                    foodpackdetail.specName = food.specName;
                    foodpackdetail.quantity = food.quantity;
                    foodpackdetail.addPrice = food.price;
                    foodpackdetail.setPrice = food.price;
                    foodpackdetail.operate = "删除";
                    foodpackdetail.packId = foodpack.id;
                    foodpackdetail.isDelete = false;
                    foodpack.packdetails.Add(foodpackdetail);                    
                }
            }
            this.binDetail.DataSource = foodpack.packdetails;
        }
    }
}
